package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import org.gdstash.character.GDCharUID;
import org.gdstash.db.DBShrine;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharShrineAct5Pane extends GDAbstractShrinePane {
    private JCheckBox cbGloomwaldCrossing;
    private JCheckBox cbAncientGrove;
    private JCheckBox cbUgdenbogKrVall;
    private JCheckBox cbTombOfUgdal;
    private JCheckBox cbBarrowholmMine;
    private JCheckBox cbDesolateWastes;
    private JCheckBox cbMalmouth;
    private JCheckBox cbRansackedLighthouse;
    private JCheckBox cbCrownHill;
    private JCheckBox cbInfestation;
    private JCheckBox cbSanctumOfTheChosen;
    private int difficulty;
    private boolean changed;

    public GDCharShrineAct5Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbGloomwaldCrossing).addComponent(this.cbAncientGrove).addComponent(this.cbUgdenbogKrVall).addComponent(this.cbTombOfUgdal).addComponent(this.cbBarrowholmMine).addComponent(this.cbDesolateWastes).addComponent(this.cbMalmouth).addComponent(this.cbRansackedLighthouse).addComponent(this.cbCrownHill).addComponent(this.cbInfestation).addComponent(this.cbSanctumOfTheChosen));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbGloomwaldCrossing)).addGroup(layout.createParallelGroup().addComponent(this.cbAncientGrove)).addGroup(layout.createParallelGroup().addComponent(this.cbUgdenbogKrVall)).addGroup(layout.createParallelGroup().addComponent(this.cbTombOfUgdal)).addGroup(layout.createParallelGroup().addComponent(this.cbBarrowholmMine)).addGroup(layout.createParallelGroup().addComponent(this.cbDesolateWastes)).addGroup(layout.createParallelGroup().addComponent(this.cbMalmouth)).addGroup(layout.createParallelGroup().addComponent(this.cbRansackedLighthouse)).addGroup(layout.createParallelGroup().addComponent(this.cbCrownHill)).addGroup(layout.createParallelGroup().addComponent(this.cbInfestation)).addGroup(layout.createParallelGroup().addComponent(this.cbSanctumOfTheChosen));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbAncientGrove);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbUgdenbogKrVall);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbTombOfUgdal);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbBarrowholmMine);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbDesolateWastes);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbMalmouth);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbRansackedLighthouse);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbCrownHill);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbInfestation);
        layout.linkSize(0, this.cbGloomwaldCrossing, this.cbSanctumOfTheChosen);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbAncientGrove);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbUgdenbogKrVall);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbTombOfUgdal);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbBarrowholmMine);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbDesolateWastes);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbMalmouth);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbRansackedLighthouse);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbCrownHill);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbInfestation);
        layout.linkSize(1, this.cbGloomwaldCrossing, this.cbSanctumOfTheChosen);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT5"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbGloomwaldCrossing == null) {
            (this.cbGloomwaldCrossing = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbGloomwaldCrossing.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_GLOOMWALD_CROSSING"));
        this.cbGloomwaldCrossing.setFont(fntCheck);
        if (this.cbAncientGrove == null) {
            (this.cbAncientGrove = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAncientGrove.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_ANCIENT_GROVE"));
        this.cbAncientGrove.setFont(fntCheck);
        if (this.cbUgdenbogKrVall == null) {
            (this.cbUgdenbogKrVall = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbUgdenbogKrVall.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_UGDENBOG_KRVALL"));
        this.cbUgdenbogKrVall.setFont(fntCheck);
        if (this.cbTombOfUgdal == null) {
            (this.cbTombOfUgdal = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTombOfUgdal.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_TOMB_OF_UGDAL"));
        this.cbTombOfUgdal.setFont(fntCheck);
        if (this.cbBarrowholmMine == null) {
            (this.cbBarrowholmMine = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBarrowholmMine.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_BARROWHOLM_MINE"));
        this.cbBarrowholmMine.setFont(fntCheck);
        if (this.cbDesolateWastes == null) {
            (this.cbDesolateWastes = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDesolateWastes.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_DESOLATE_WASTES"));
        this.cbDesolateWastes.setFont(fntCheck);
        if (this.cbMalmouth == null) {
            (this.cbMalmouth = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbMalmouth.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_MALMOUTH"));
        this.cbMalmouth.setFont(fntCheck);
        if (this.cbRansackedLighthouse == null) {
            (this.cbRansackedLighthouse = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbRansackedLighthouse.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_RANSACKED_LIGHTHOUSE"));
        this.cbRansackedLighthouse.setFont(fntCheck);
        if (this.cbCrownHill == null) {
            (this.cbCrownHill = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCrownHill.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_CROWN_HILL"));
        this.cbCrownHill.setFont(fntCheck);
        if (this.cbInfestation == null) {
            (this.cbInfestation = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbInfestation.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_INFESTATION"));
        this.cbInfestation.setFont(fntCheck);
        if (this.cbSanctumOfTheChosen == null) {
            (this.cbSanctumOfTheChosen = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSanctumOfTheChosen.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_05_SANCTUM_OF_THE_CHOSEN"));
        this.cbSanctumOfTheChosen.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbGloomwaldCrossing.setSelected(selected);
        this.cbAncientGrove.setSelected(selected);
        this.cbUgdenbogKrVall.setSelected(selected);
        this.cbTombOfUgdal.setSelected(selected);
        this.cbBarrowholmMine.setSelected(selected);
        this.cbDesolateWastes.setSelected(selected);
        this.cbMalmouth.setSelected(selected);
        this.cbRansackedLighthouse.setSelected(selected);
        this.cbCrownHill.setSelected(selected);
        this.cbInfestation.setSelected(selected);
        this.cbSanctumOfTheChosen.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbGloomwaldCrossing.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_GLOOMWALD_CROSSING);
        }
        if (this.cbAncientGrove.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_ANCIENT_GROVE);
        }
        if (this.cbUgdenbogKrVall.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_UGDENBOG_KR_VALL);
        }
        if (this.cbTombOfUgdal.isSelected() == found) {
            if (this.difficulty == 0) {
                list.add(GDCharShrineList.UID_SHRINE_05_TOMB_OF_UGDAL);
            }
            if (this.difficulty == 1) {
                list.add(GDCharShrineList.UID_SHRINE_05_TOMB_OF_UGDAL_ELITE);
            }
            if (this.difficulty == 2) {
                list.add(GDCharShrineList.UID_SHRINE_05_TOMB_OF_UGDAL);
            }
        }
        if (this.cbBarrowholmMine.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_BARROWHOLM_MINE);
        }
        if (this.cbDesolateWastes.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_DESOLATE_WASTES);
        }
        if (this.cbMalmouth.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_MALMOUTH);
        }
        if (this.cbRansackedLighthouse.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_RANSACKED_LIGHTHOUSE);
        }
        if (this.cbCrownHill.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_CROWN_HILL);
        }
        if (this.cbInfestation.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_INFESTATION);
        }
        if (this.cbSanctumOfTheChosen.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_05_SANCTUM_OF_THE_CHOSEN);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrinee01.dbr", this.difficulty);
        this.cbGloomwaldCrossing.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinef03.dbr", this.difficulty);
        this.cbAncientGrove.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen09.dbr", this.difficulty);
        this.cbUgdenbogKrVall.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinee02.dbr", this.difficulty);
        this.cbTombOfUgdal.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen08.dbr", this.difficulty);
        this.cbBarrowholmMine.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen06.dbr", this.difficulty);
        this.cbDesolateWastes.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinef01.dbr", this.difficulty);
        this.cbMalmouth.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen02.dbr", this.difficulty);
        this.cbRansackedLighthouse.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinef02.dbr", this.difficulty);
        this.cbCrownHill.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen11.dbr", this.difficulty);
        this.cbInfestation.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrines03.dbr", this.difficulty);
        this.cbSanctumOfTheChosen.setEnabled(info.active && this.difficulty == 2);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_GLOOMWALD_CROSSING)) {
                this.cbGloomwaldCrossing.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_ANCIENT_GROVE)) {
                this.cbAncientGrove.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_UGDENBOG_KR_VALL)) {
                this.cbUgdenbogKrVall.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_TOMB_OF_UGDAL)) {
                this.cbTombOfUgdal.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_TOMB_OF_UGDAL_ELITE)) {
                this.cbTombOfUgdal.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_BARROWHOLM_MINE)) {
                this.cbBarrowholmMine.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_DESOLATE_WASTES)) {
                this.cbDesolateWastes.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_MALMOUTH)) {
                this.cbMalmouth.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_RANSACKED_LIGHTHOUSE)) {
                this.cbRansackedLighthouse.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_CROWN_HILL)) {
                this.cbCrownHill.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_INFESTATION)) {
                this.cbInfestation.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_05_SANCTUM_OF_THE_CHOSEN)) {
                this.cbSanctumOfTheChosen.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct5Pane.this.changed = true;
        }
    }
}
